package com.ebupt.migu.music.common.webconfig;

import java.util.ArrayList;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
// @ConfigurationProperties(prefix = "spring.elasticsearch")
public class EsConfig {

    @Value("${spring.elasticsearch.hosts}")
    private String hosts; // 集群地址，多个用,隔开
    @Value("${spring.elasticsearch.port}")
    private int port; // 使用的端口号
    @Value("${spring.elasticsearch.schema}")
    private String schema; // 使用的协议
    private ArrayList<HttpHost> hostList = null;

    @Value("${spring.elasticsearch.connect-time-out}")
    private int connectTimeOut; // 连接超时时间
    @Value("${spring.elasticsearch.socket-time-out}")
    private int socketTimeOut; // 连接超时时间
    @Value("${spring.elasticsearch.connection-request-time-out}")
    private int connectionRequestTimeOut; // 获取连接的超时时间

    @Value("${spring.elasticsearch.max-connect-num}")
    private int maxConnectNum; // 最大连接数
    @Value("${spring.elasticsearch.max-connect-per-route}")
    private int maxConnectPerRoute; // 最大路由连接数

    @Bean
    public RestHighLevelClient client() {
        hostList = new ArrayList<>();
        String[] hostStrs = hosts.split(",");
        for (String host : hostStrs) {
            hostList.add(new HttpHost(host, port, schema));
        }
        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(new RequestConfigCallback() {
            @Override
            public Builder customizeRequestConfig(Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });
        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}